package cn.dnui_xcz826.dnuifood_16110100826.controller;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import cn.dnui_xcz826.dnuifood_16110100826.R;
import cn.dnui_xcz826.dnuifood_16110100826.adapter.FoodListAdapter;
import cn.dnui_xcz826.dnuifood_16110100826.bean.FoodBean;
import cn.dnui_xcz826.dnuifood_16110100826.listener.onRetrofitListener;
import cn.dnui_xcz826.dnuifood_16110100826.model.FoodModel;

public class SearchResultActivity extends BaseActivity implements onRetrofitListener<List<FoodBean>>
{
    private RecyclerView recyclerView;
    private ImageView go_back;
    private String search="";

    void initViews() {
        setLayout(R.layout.activity_search_result);
        go_back=findViewById(R.id.goback);
        recyclerView = (RecyclerView)findViewById(R.id.rv_sr);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
    void initEvents(){
        //处理返回按钮
        go_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    void initDatas() {
        search=getIntent().getStringExtra("search");
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodBySearch(search,this);
    }

    @Override
    public void onSuccess(List<FoodBean> list, int flag) {
        if (list!=null&&list.size() > 0) {
            FoodListAdapter adapter = new FoodListAdapter(SearchResultActivity.this, list, R.layout.food_item);
            recyclerView.setAdapter(adapter);
        }
        else
        {
            Toast.makeText(SearchResultActivity.this, "没有搜索结果", Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(SearchResultActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();
    }


}